package com.vjwealth.android.nos.java.widget;

import android.content.Context;
import android.text.InputFilter;
import android.text.Spanned;
import android.util.AttributeSet;
import android.widget.EditText;

import com.vjwealth.android.nos.java.utils.StringUtils;

/**
 * projectName NOS_Android
 * date 2017/7/26
 * description 过滤特殊字符的输入
 */

public class MessyEditText extends EditText {

    private InputFilter mMessyFilter;
    private InputFilter.LengthFilter mLengthFilter;

    public MessyEditText(Context context) {
        super(context);
        init();
    }

    public MessyEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MessyEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init(){
        mMessyFilter = new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                String inputS = String.valueOf(source);
                if(StringUtils.isMessyCode(inputS)){
                    return "";
                }
                return source;
            }
        };
        this.setFilters(new InputFilter[]{mMessyFilter, new InputFilter.LengthFilter(22)});
    }

    public void setLengthFilter(int lengthFilter){
        if(lengthFilter <= 0) lengthFilter = 22;
        mLengthFilter = new InputFilter.LengthFilter(lengthFilter);
        this.setFilters(new InputFilter[]{mMessyFilter, mLengthFilter});
    }

//    public abstract CharSequence filter (
//            CharSequence source,  //输入的文字
//            int start,  //开始位置
//            int end,  //结束位置
//            Spanned dest, //当前显示的内容
//            int dstart,  //当前开始位置
//            int dend //当前结束位置
//    );
}
